package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.BtyPerformancePoint;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyPerformancePointMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.AttendAuditBtyAreaParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyPerformanceMonthInformationParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyPerformancePointParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyPerformanceReceiveParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyPerformanceMonthInformationResult;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyPerformancePointResult;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyPerformanceSelectResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyPerformanceMonthCheckService;
import cn.stylefeng.guns.sys.modular.system.service.BtyPerformanceMonthInformationService;
import  cn.stylefeng.guns.sys.modular.system.service.BtyPerformancePointService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wjk
 * @since 2022-02-10
 */
@Service
public class BtyPerformancePointServiceImpl extends ServiceImpl<BtyPerformancePointMapper, BtyPerformancePoint> implements BtyPerformancePointService {

    @Autowired
    private BtyPerformanceMonthCheckService btyPerformanceMonthCheckService;

    @Autowired
    private BtyPerformanceMonthInformationService btyPerformanceMonthInformationService;
    @Override
    public void add(BtyPerformancePointParam param){
        BtyPerformancePoint entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BtyPerformancePointParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BtyPerformancePointParam param){
        BtyPerformancePoint oldEntity = getOldEntity(param);
        BtyPerformancePoint newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BtyPerformancePointResult findBySpec(BtyPerformancePointParam param){
        return null;
    }

    @Override
    public List<BtyPerformancePointResult> findListBySpec(BtyPerformancePointParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyPerformancePointParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 3.4.13督导或大店长或营运经理根据手机号，月份查询名下所有的门店信息及级别
     *
     * @author ws
     * @Date 2022-02-10
     */
    @Override
    public LayuiPageInfo findPageByAccountTime(String account,BtyPerformanceReceiveParam param) {

        if (ObjectUtil.isEmpty(param.getTime())) {
            Map<String,String> mapTime = this.btyPerformanceMonthCheckService.getPerformanceDay();
            param.setTime(mapTime.get("lastYearMonth"));
        }
        // 根据手机号判断用户角色
        Map<String,String> map = this.btyPerformanceMonthCheckService.checkIdentity(account);
        Page pageContext = getPageContext();
        if(ToolUtil.isNotEmpty(map.get("userZwAllStr"))){
            if(map.get("userZwAllStr").indexOf("督导")>-1){
                IPage page = this.baseMapper.pageByDdPhone(pageContext, account,param);
                return LayuiPageFactory.createPageInfo(page);
            }

            if(map.get("userZwAllStr").indexOf("大店长")>-1){
                IPage page = this.baseMapper.pageByDdzPhone(pageContext, account,param);
                return LayuiPageFactory.createPageInfo(page);
            }
            if(map.get("userZwAllStr").indexOf("营运经理")>-1){
                IPage page = this.baseMapper.pageByYyjlPhone(pageContext, account,param);
                return LayuiPageFactory.createPageInfo(page);
            }
        }
        return LayuiPageFactory.createPageInfo(LayuiPageFactory.defaultPage());
    }



    @Override
    public Map<String, String> getAttendMonth() {
        Map<String,String> mapParam = new HashMap<String,String>();
        //默认创建当前日期的上个月
        Date date = new Date();
        SimpleDateFormat daySdf = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat lastMonthSdf = new SimpleDateFormat("yyyy-MM");
        String lastYearMonth = daySdf.format(this.calculateMonth(date,-1));
        mapParam.put("lastYearMonth",lastYearMonth);
        mapParam.put("lastMonth",lastMonthSdf.format(this.calculateMonth(date,-1)));
        return mapParam;
    }

    /**
     * 13  查看总驳回原因
     *
     * @author wjk
     * @Date 2022-04-07
     */
    @Override
    public BtyPerformanceMonthInformationResult tddgetDismissedTotal(String account, BtyPerformanceMonthInformationParam param) {

        BtyPerformanceMonthInformationResult btyPerformanceMonthInformationResult = this.baseMapper.tddgetDismissedTotal(param);
        if(ToolUtil.isEmpty(btyPerformanceMonthInformationResult)){
            return new BtyPerformanceMonthInformationResult();
        }
        return btyPerformanceMonthInformationResult;
    }


    private Date calculateMonth(Date date, int i) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(date);
        rightNow.add(Calendar.MONTH, i);
        Date date1  = rightNow.getTime();
        return date1;
    }

    private Serializable getKey(BtyPerformancePointParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyPerformancePoint getOldEntity(BtyPerformancePointParam param) {
        return this.getById(getKey(param));
    }

    private BtyPerformancePoint getEntity(BtyPerformancePointParam param) {
        BtyPerformancePoint entity = new BtyPerformancePoint();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }







    /**
     * 15 运营经理查看 门店总数，可做绩效 的门店数量
     *
     * @author wjk
     * @Date 2022-04-06
     */
    @Override
    public Map<String, String> tYyjlgetShopSumMonth(String account, BtyPerformanceReceiveParam param) {

        Map<String, String> map = new HashMap<String, String>();
        map.put("conAll","0");
        map.put("con","0");

        if (ObjectUtil.isEmpty(param.getTime())) {
            Map<String,String> mapTime = this.btyPerformanceMonthCheckService.getPerformanceDay();
            param.setTime(mapTime.get("lastYearMonth"));
        }
        // 根据手机号判断用户角色
        Map<String,String> mapCheck = this.btyPerformanceMonthCheckService.checkIdentity(account);
        Page pageContext = getPageContext();
        if(ToolUtil.isNotEmpty(mapCheck.get("userZwAllStr"))){
            if(mapCheck.get("userZwAllStr").indexOf("营运经理")>-1){
                param.setCheckStatus("");
                IPage page = this.baseMapper.tyyjlgetShopStateListPage(pageContext, account,param);
                LayuiPageInfo layuiPageInfo =  LayuiPageFactory.createPageInfo(page);
                map.put("conAll",String.valueOf(layuiPageInfo.getCount()));

                param.setJxStatus("0");
                IPage page2 = this.baseMapper.tyyjlgetShopStateListPage(pageContext, account,param);
                LayuiPageInfo layuiPageInfo2 =  LayuiPageFactory.createPageInfo(page2);
                map.put("con",String.valueOf(layuiPageInfo2.getCount()));
            }
        }
        return  map;

    }


    /**
     * 16 运营经理，待审核，已驳回，已通过的店列表
     *
     * @author wjk
     * @Date 2022-04-06
     */

    @Override
    public LayuiPageInfo tyyjlgetShopStateListPage(String account, BtyPerformanceReceiveParam param) {
        if (ObjectUtil.isEmpty(param.getTime())) {
            Map<String,String> mapTime = this.btyPerformanceMonthCheckService.getPerformanceDay();
            param.setTime(mapTime.get("lastYearMonth"));
        }
        // 根据手机号判断用户角色
        Map<String,String> map = this.btyPerformanceMonthCheckService.checkIdentity(account);
        Page pageContext = getPageContext();
        if(ToolUtil.isNotEmpty(map.get("userZwAllStr"))){
            if(map.get("userZwAllStr").indexOf("营运经理")>-1){
                IPage page = this.baseMapper.tyyjlgetShopStateListPage(pageContext, account,param);
                return LayuiPageFactory.createPageInfo(page);
            }
        }
        return LayuiPageFactory.createPageInfo(LayuiPageFactory.defaultPage());
    }

    /**
     * 17 运营经理查询下属的督导列表,不分页全部
     * @author wjk
     * @Date 2022-04-06
     */
    @Override
    public  List<BtyPerformanceSelectResult> tyyjlgetHisDyList(String account) {
        AttendAuditBtyAreaParam param = new AttendAuditBtyAreaParam();
        param.setYydh(account);
        return this.btyPerformanceMonthInformationService.findListDd(param);
    }



}
